package com.im.strive.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.im.strive.MainActivity;
import com.im.strive.R;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

import com.im.strive.adapter.PartAdapter;
import com.im.strive.model.beans.Results;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayout;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by jiangwei on 16/4/23.
 */
public class MainFragment extends Fragment {
    private static final String ARG_TITLE = "title";
    private String mTitle;

    private SwipyRefreshLayout swipyRefreshLayout;
    private RecyclerView recyclerview;

    private PartAdapter partAdapter;

    private List<Results> home_list = new ArrayList<Results>();



    public static MainFragment getInstance(String title) {
        MainFragment fra = new MainFragment();
        Bundle bundle = new Bundle();
        bundle.putString(ARG_TITLE, title);
        fra.setArguments(bundle);
        return fra;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        mTitle = bundle.getString(ARG_TITLE);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.content_main ,container , false );

        partAdapter = new PartAdapter(getActivity(), home_list);
        initRecyclerView(view);
        initSwipyRefreshLayout(view);
        QueryHomeData();

        return view;
    }

    private void initRecyclerView(View view) {

        recyclerview = (RecyclerView) view.findViewById(R.id.recyclerView);
        switch (mTitle) {
            case "聚合集锦":
                recyclerview.setLayoutManager(new LinearLayoutManager(recyclerview.getContext()));
                partAdapter = new PartAdapter(getActivity(), home_list);
                recyclerview.setAdapter(partAdapter);
                Log.i("HomePage" , "Complated");
                break;
//            case "干货":
//                recyclerview.setLayoutManager(new LinearLayoutManager(recyclerview.getContext()));
//                realAdapter = new RealAdapter(getActivity(), null);
//                recyclerview.setAdapter(realAdapter);
//                break;
//            case "妹纸":
//                recyclerview.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
//                girlyAdapter = new GirlyAdapter(getActivity(), null);
//                recyclerview.setAdapter(girlyAdapter);
//                break;
        }



    }

    private void initSwipyRefreshLayout(View view) {
        swipyRefreshLayout = (SwipyRefreshLayout) view.findViewById(R.id.swipyrefreshlayout);
        swipyRefreshLayout.setColorSchemeResources(
                android.R.color.holo_blue_light,
                android.R.color.holo_red_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_green_light);
        swipyRefreshLayout.setDirection(SwipyRefreshLayoutDirection.BOTH);

        swipyRefreshLayout.setOnRefreshListener(new SwipyRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(SwipyRefreshLayoutDirection direction) {
                Log.d("MainActivity", "Refresh triggered at "
                        + (direction == SwipyRefreshLayoutDirection.TOP ? "top" : "bottom"));
                Observable.timer(2, TimeUnit.SECONDS)
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Action1<Long>() {
                            @Override
                            public void call(Long aLong) {
                                swipyRefreshLayout.setRefreshing(false);
                            }
                        });
                loadData(direction == SwipyRefreshLayoutDirection.TOP ? true : false);

            }
        });
    }

    private void loadData(boolean isTop) {

        switch (mTitle) {
            case "首页":
//                if (isTop) {
//                    NOW_PAGE_FI = 1;
//                }
//                getDataResults("all", fi_num, NOW_PAGE_FI, isTop);
                QueryHomeData();
                home_list.clear();
                break;
//            case "干货":
//                FRESH_GANHUO_TIME = 0;
//                ganhuo_real_list.clear();
//                getDataResults("Android", gh_num, NOW_PAGE_GH, isTop);
//                getDataResults("iOS", gh_num, NOW_PAGE_GH, isTop);
//                getDataResults("前端", gh_num, NOW_PAGE_GH, isTop);
//                getDataResults("拓展资源", gh_num, NOW_PAGE_GH, isTop);
//                break;
//            case "妹纸":
//                if (isTop) {
//                    NOW_PAGE_MZ = 1;
//                }
//                getDataResults("福利", mz_num, NOW_PAGE_MZ, isTop);
//                break;
        }

    }

    private void QueryHomeData() {

        BmobQuery<Results> query = new BmobQuery<Results>();
        query.setLimit(5);
        query.setMaxCacheAge(TimeUnit.DAYS.toMillis(2));//此表示缓存一天
        query.order("-createdAt");
        query.findObjects(getActivity(), new FindListener<Results>() {
            @Override
            public void onSuccess(List<Results> list) {
                for (Results result : list) {
                    home_list.add(result);

                }

                partAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(int i, String s) {

            }
        });
    }
}
